package com.data4.utils.controller;

import com.data4.feignapi.dto.Result;
import com.data4.utils.config.MinioConfig;
import com.data4.utils.service.OSSService;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Slf4j
@RestController
@Tag(name = "文件上传")
@RequestMapping("/util")
public class OSSController {
    @Resource
    private OSSService ossService;
    @Resource
    private MinioConfig minioConfig;

    /**
     * 文件上传   接受前端压缩后的文件，例如视频与图片都由压缩后上传，这样网络io减少，同时减少服务器压力
     *
     * @param file
     */

    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        return ossService.uploadFile(minioConfig.getBucketName(), file);
    }

    /**
     * 删除OSS中的文件
     *
     * @param fileName
     */
    @Operation(summary = "删除文件")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("fileName") String fileName) {
        return ossService.deleteFile(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @Operation(summary = "获取文件信息")
    @GetMapping("/info")
    public Result getFileStatusInfo(@RequestParam("fileName") String fileName)
            throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ossService.getFileInfo(minioConfig.getBucketName(), fileName);
    }


}

